package com.lc.demo;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import static com.lc.executor.impl.DefaultExecutor.getConn;


public class JdbcDemo {

    public List<User> selectUser() {
        List<User> users = new ArrayList<>();
        ResultSet rs;
        Connection conn = getConn();
        String sql = "select id,name from user where name = ?";
        try {
            //Object id=1;
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,"hy");
            /*ps.setInt(2,id);*/
            rs = ps.executeQuery();
            while (rs.next()) {
                User user = new User(rs.getInt("id"), rs.getString("name"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }

    public void insertUser() throws SQLException {
        Connection conn = getConn();
        String sql = "insert into sys_user(id,name) values('001','zs')";
        PreparedStatement ps = conn.prepareStatement(sql);
        int execute = ps.executeUpdate();
        System.out.println("res:" + execute);
    }

    public static void main(String[] args) throws Exception {
        JdbcDemo demo = new JdbcDemo();
        List<User> users = demo.selectUser();
        System.out.println(users);
        //demo.insertUser();
    }
}
